﻿using AuthCenter.CSM.Common;
using AuthCenter.CSM.Dto;
using AuthCenter.Data.Database;
using AuthCenter.Tools.Password;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Http;

namespace AuthCenter.CSM.plugins
{
    /// <summary>
    /// 应用专属接口
    /// </summary>
    internal class AppPlug : AuthCenterPluginBase
    {
        protected override string ListenUri { get; } = HttpCommon.SiteName + "/app";
        protected override async Task OnRequest(AuthCenterSession session)
        {
            if (session.IsPost(HttpCommon.SiteName+"/app"))
            {
                var output = new AuthCenterOutput();
                output.Success = true;

                if (!session.IsAppAuthenticated)
                {
                    output.Success = false;
                }

                if (output.Success)
                {
                    var data = await session.GetData<AuthCenterInput>();
                    Guid userId;
                    if (session.IsAuthenticated)
                    {
                        switch (data.Method)
                        {
                            case "set-permission":
                                var permission_content = data.GetText("permissions");
                                session.db_auth.UpdatePermission(session.UserID, session.AppID, permission_content);
                                session.db_log.Record("set-permission", "更新权限", session.AppID, session.UserID);
                                goto SEND_APP_REP;
                            case "get-uid":
                                output.Params["uid"] = session.UserID.ToString();
                                goto SEND_APP_REP;
                            case "get-secret":
                                var needget_secret = data.GetNumber("no");
                                if (needget_secret >= 1 && needget_secret <= 7)
                                {
                                    output.Params["user"] = session.db_auth.GetAccountName(session.UserID);
                                    output.Params["secret"] = session.db_auth.GetSecret(session.UserID, needget_secret);
                                }
                                else output.Success = false;
                                goto SEND_APP_REP;
                        }
                    }

                    switch (data.Method)
                    {
                        case "update-pass":
                            //B1 格式密码更新
                            userId = session.db_auth.GetUserID(data.GetText("user"));
                            if (userId == Guid.Empty) output.Success = false;
                            else
                            {
                                session.db_auth.UpdateUserPassword(userId, data.GetText("password"), "B");
                            }
                            break;
                        case "set-secret":
                            var needset_secret = data.GetNumber("no");
                            if (needset_secret <= 7 && needset_secret >= 1)
                            {
                                userId = session.db_auth.GetUserID(data.GetText("user"));
                                if (userId == Guid.Empty) output.Success = false;
                                else
                                {
                                    session.db_auth.SetSecret(userId, data.GetText("secret"), needset_secret);
                                }
                            }
                            else output.Success = false;
                            break;
                        case "get-secret":
                            var needget_secret = data.GetNumber("no");
                            if (needget_secret <= 7 && needget_secret >= 1)
                            {
                                userId = session.db_auth.GetUserID(data.GetText("user"));
                                if (userId == Guid.Empty) output.Success = false;
                                else
                                {
                                    output.Params["secret"] = session.db_auth.GetSecret(userId, needget_secret);
                                }
                            }
                            else output.Success = false;
                            break;
                        default:
                            output.Success = false;
                            break;
                    }

                }

            SEND_APP_REP:
                await session.Send(200, output)
                    .AnswerAsync();
            }

            await session.InvokeNext();
        }
    }
}
